package com.cyan.teamquestionnaire.controller;

import com.cyan.teamquestionnaire.beans.HttpResponseEntity;
import com.cyan.teamquestionnaire.beans.PageInfo;
import com.cyan.teamquestionnaire.entity.AnswerSheet;
import com.cyan.teamquestionnaire.entity.Group;
import com.cyan.teamquestionnaire.entity.User;
import com.cyan.teamquestionnaire.service.GroupService;
import com.cyan.teamquestionnaire.utils.Constants;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class GroupController {

    // 返回信息
    @Resource
    private HttpResponseEntity httpResponseEntity;

    @Resource
    private GroupService groupService;

    /**
     * 获得群组——不分页
     * 传入name Groupname
     */
    @RequestMapping(value = "/getGroupList", method = RequestMethod.POST)
    public HttpResponseEntity getGroupList(@RequestBody Group group) {
        httpResponseEntity.setData(groupService.getGroupList(group));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }


    /**
     * 获得群组——分页
     * 传入name  对groupname进行模糊查询
     */
    @RequestMapping(value = "/getGroupListY", method = RequestMethod.POST)
    public HttpResponseEntity getGroupListY(@RequestBody PageInfo<Group> pageInfo) {
        httpResponseEntity.setData(groupService.getGroupListY(pageInfo));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

    /**
     * 获得群组——分页
     * 传入gid  对用户名进行模糊查询
     */
    @RequestMapping(value = "/usersInGroupFuzzyY", method = RequestMethod.POST)
    public HttpResponseEntity usersInGroupFuzzyY(@RequestBody PageInfo<User> pageInfo) {
        httpResponseEntity.setData(groupService.usersInGroupFuzzyY(pageInfo));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

    /**
     * 新增群组
     * 传入对象group
     */
    @RequestMapping(value = "/createGroup", method = RequestMethod.POST)
    public HttpResponseEntity createGroup(@RequestBody Group group) {
        groupService.createGroup(group);
        httpResponseEntity.setData(null);
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.CREATE_MESSAGE);
        return httpResponseEntity;
    }

    /**
     * 删除群组,同时在群组里面的用户信息也删除(group_user)
     * 传入对象gid
     */
    @RequestMapping(value = "/deleteGroup", method = RequestMethod.POST)
    public HttpResponseEntity deleteGroup(String gId) {
        boolean flag = groupService.deleteGroup(gId);
        if (flag) {
            httpResponseEntity.setCode(Constants.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constants.DELETE_Q_TRUE);
        } else {
            httpResponseEntity.setCode(Constants.EXIST_CODE);
            httpResponseEntity.setMessage(Constants.Group_Have_Questionnaire);
        }
        httpResponseEntity.setData(null);

        return httpResponseEntity;
    }

    /**
     * 修改群组,修改名字
     * 传入对象gid，group_name
     */
    @RequestMapping(value = "/modifyGroup", method = RequestMethod.POST)
    public HttpResponseEntity modifyGroup(@RequestBody Group group) {
        groupService.modifyGroup(group);
        httpResponseEntity.setData(null);
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.MODIFY_SUCCESS);
        return httpResponseEntity;
    }

    /**
     * 返回这个租户管理下的用户还没加入群组的用户
     * 要想群组加入用户，首先要选择出来这个租户的所有用户，然后选择出来所有这个群组里面的用户，两者做差集
     * 传入对象gid，租户名字name,对名字进行模糊查询
     * 封装在pageinfo里面
     */
    @RequestMapping(value = "/getRestUser", method = RequestMethod.POST)
    public HttpResponseEntity getRestUser(@RequestBody PageInfo<User> pageInfo) {
        httpResponseEntity.setData(groupService.getRestUser(pageInfo));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }


    /**
     * 将用户加入到群组，操作的是group_user表
     * 传入对象gid，user_name
     */
    @RequestMapping(value = "/addUserToGroup", method = RequestMethod.POST)
    public HttpResponseEntity addUserToGroup(@RequestBody Map<String, Object> map) {
        String gId = (String) map.get("gId");
        String username = (String) map.get("username");

        boolean flag = groupService.addUserToGroup(username, gId);

        if (flag) {
            httpResponseEntity.setCode(Constants.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constants.CREATE_MESSAGE);
        } else {
            httpResponseEntity.setCode(Constants.EXIST_CODE);
            httpResponseEntity.setMessage(Constants.Group_User_Repeat);
        }
        httpResponseEntity.setData(null);
        return httpResponseEntity;
    }


    /**
     * 删除群组中的用户，操作的是group_user表
     * 传入对象gid，user_name
     */
    @RequestMapping(value = "/deleteUserFromGroup", method = RequestMethod.POST)
    public HttpResponseEntity deleteUserFromGroup(@RequestBody Map<String, Object> map) {
        String gId = (String) map.get("gId");
        String username = (String) map.get("username");

        boolean flag = groupService.deleteUserFromGroup(username, gId);

        if (flag) {
            httpResponseEntity.setCode(Constants.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constants.DELETE_Q_TRUE);
        } else {
            httpResponseEntity.setCode(Constants.EXIST_CODE);
            httpResponseEntity.setMessage(Constants.Group_User_Not);
        }
        httpResponseEntity.setData(null);
        return httpResponseEntity;
    }

    // 租户下所有的群组数
    @RequestMapping(value = "/cntGInTenant", method = RequestMethod.POST)
    public HttpResponseEntity cntGInTenant(String name) {
        httpResponseEntity.setData(groupService.countGroupInTenant(name));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

    /**
     * 获得租户下面的群组——分页
     * 传入name  对groupname进行模糊查询
     */
    @RequestMapping(value = "/GroupInTenantFuzzy", method = RequestMethod.POST)
    public HttpResponseEntity GroupInTenantFuzzy(@RequestBody PageInfo<Group> pageInfo) {
        httpResponseEntity.setData(groupService.GroupInTenantFuzzy(pageInfo));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

}
